//
//  OMPersonalCenterBar.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/17.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMPersonalCenterBar.h"

@interface OMPersonalCenterBar ()
@property (nonatomic, strong) UILabel *nicknameLb;
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UIButton *messageCenterButton;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIButton *toLoginButton;
@property (nonatomic, strong) UIImageView *rippleView;

@end

@implementation OMPersonalCenterBar

+ (instancetype)customeBar
{
    OMPersonalCenterBar *bar = [OMPersonalCenterBar new];
    bar.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SCREEN_WIDTH/375.f*225+SM_SIZE(70));
    [bar configure];
    return bar;
}

CGFloat avatarWidth = 0;
- (void)configure
{

    _backgroundImageView = [[UIImageView alloc]init];
    _backgroundImageView.image = SMPNGImage(@"personalbg_375x225_");
    _backgroundImageView.userInteractionEnabled = YES;
    [self addSubview:_backgroundImageView];
    [_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@(SM_SIZE(0)));
        make.height.equalTo(@(SM_SCREEN_WIDTH/375.f*225));
    }];


    avatarWidth = SM_SIZE(160);
    _avatarImageView = [UIImageView new];
    _avatarImageView.image = [UIImage st_imageNamed:@"ic_personal_avatar_placehold.jpg" type:@""];
    _avatarImageView.layer.cornerRadius = avatarWidth/2;
    _avatarImageView.clipsToBounds = YES;
    _avatarImageView.layer.borderColor = SM_whiteColor.CGColor;
    _avatarImageView.layer.borderWidth = SM_SIZE(5);
    [_backgroundImageView addSubview:self.avatarImageView];
    [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_backgroundImageView.mas_centerX);
        make.centerY.equalTo(_backgroundImageView.mas_centerY).offset(SM_SIZE(-20));
        make.width.height.equalTo(@(avatarWidth));
    }];
    _avatarImageView.userInteractionEnabled = YES;
//    _avatarImageView.hidden = true;
    UITapGestureRecognizer *tapAvatar = [[UITapGestureRecognizer alloc]init];
    [_avatarImageView addGestureRecognizer:tapAvatar];
    [[tapAvatar rac_gestureSignal] subscribeNext:^(id x) {
        if ([Navigator showLoginViewIfUnLogin]) {
//            NaviPush(@"OMPersonalInfoViewController", @{@"title":@"我的信息"});
        }
    }];


    _nicknameLb = [UILabel new];
    _nicknameLb.font = SM_CFont(28);
    _nicknameLb.textColor = SM_blackColor;
    _nicknameLb.text = @"邻檬圈";
    [_backgroundImageView addSubview:_nicknameLb];
    [_nicknameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatarImageView.mas_bottom).offset(SM_SIZE(25));
        make.centerX.equalTo(self.mas_centerX);
    }];


    _toLoginButton = [UIButton new];
    [_toLoginButton setTitleColor:SM_blackColor forState:UIControlStateNormal];
    [_toLoginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_toLoginButton.titleLabel setFont:SM_CFont(32)];
    _toLoginButton.layer.cornerRadius = SM_SIZE(5);
    _toLoginButton.layer.borderColor = SM_blackColor.CGColor;
    _toLoginButton.layer.borderWidth = 1.f;
    _toLoginButton.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(5), SM_SIZE(15), SM_SIZE(5), SM_SIZE(15));
    [self addSubview:_toLoginButton];
    [_toLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarImageView.mas_bottom).offset(SM_SIZE(30));
        make.centerX.equalTo(self.mas_centerX);
        make.width.equalTo(@(SM_SIZE(150)));
        make.height.equalTo(@(SM_SIZE(50)));
    }];
    [[_toLoginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [Navigator showLoginViewIfUnLogin];
    }];

    _messageCenterButton = [UIButton new];
    [_messageCenterButton setImage:SMPNGImage(@"xiaoxizhongxin_25x25_") forState:UIControlStateNormal];
    [self addSubview:_messageCenterButton];
    [_messageCenterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-30)));
        make.top.equalTo(@(SM_SIZE(50)));
    }];
    [[_messageCenterButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([Navigator showLoginViewIfUnLogin]) {
            NaviPush(@"OMMessageCenterViewController", nil);
        }
    }];

    UIImageView *backgroundView = [UIImageView new];
    backgroundView.image = SMPNGImage(@"ic_personal_roundshape");
    [self addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(0));
        make.height.equalTo(@(SM_SIZE(80)));
        make.bottom.equalTo(@(SM_SIZE(-20)));
    }];

    UIView *floatView = [UIView new];
    floatView.backgroundColor = SM_whiteColor;
    floatView.layer.borderColor = SM_lightGrayColor.CGColor;
    floatView.layer.cornerRadius = SM_SIZE(10);
    floatView.layer.borderWidth = 0.5f;
    floatView.clipsToBounds = true;
    [self addSubview:floatView];
    [floatView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(@(SM_SIZE(-20)));
        make.height.equalTo(@(SM_SIZE(100)));
    }];

    UIView *leftView = [UIView new];
    [floatView addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@(SM_SIZE(0)));
    }];
    UIImageView *couponIcon = [UIImageView new];
    couponIcon.image = SMPNGImage(@"wodeyouhui_25x25_");
    [leftView addSubview:couponIcon];
    [couponIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(55)));
        make.centerY.equalTo(leftView.mas_centerY);
        make.width.height.equalTo(@(SM_SIZE(50)));
    }];
    UILabel *couponLabel = [UILabel new];
    couponLabel.font = SM_CFont(30);
    couponLabel.textColor = SM_blackColor;
    couponLabel.text = @"我的优惠";
    [leftView addSubview:couponLabel];
    [couponLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(couponIcon.mas_right).offset(SM_SIZE(20));
        make.centerY.equalTo(leftView.mas_centerY);
        make.right.equalTo(@(SM_SIZE(-55)));
    }];
    leftView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc]init];
    [[tapLeft rac_gestureSignal]subscribeNext:^(id x) {
        if ([Navigator showLoginViewIfUnLogin]) {
            NaviPush(@"OMPersonalCenterCheapViewController", @{@"title":couponLabel.text});
        }
    }];
    [leftView addGestureRecognizer:tapLeft];

    UIView *line = [UIView new];
    line.backgroundColor = SM_placeholdColor;
    [floatView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftView.mas_right);
        make.width.equalTo(@1);
        make.height.equalTo(@(SM_SIZE(50)));
        make.centerY.equalTo(floatView.mas_centerY);
    }];

    UIView *rightView = [UIView new];
    [floatView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(@(SM_SIZE(0)));
        make.left.equalTo(leftView.mas_right);
    }];
    UIImageView *refundIcon = [UIImageView new];
    refundIcon.image = SMPNGImage(@"wodetuikuan_25x25_");
    [rightView addSubview:refundIcon];
    [refundIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(55)));
        make.centerY.equalTo(rightView.mas_centerY);
        make.width.height.equalTo(@(SM_SIZE(50)));
    }];
    UILabel *refundLabel = [UILabel new];
    refundLabel.font = SM_CFont(30);
    refundLabel.textColor = SM_blackColor;
    refundLabel.text = @"我的退款";
    [rightView addSubview:refundLabel];
    [refundLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(refundIcon.mas_right).offset(SM_SIZE(20));
        make.centerY.equalTo(rightView.mas_centerY);
        make.right.equalTo(@(SM_SIZE(-55)));
    }];

    rightView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc]init];
    [[tapRight rac_gestureSignal]subscribeNext:^(id x) {
        if ([Navigator showLoginViewIfUnLogin]) {
            NaviPush(@"OMOrderRefundListViewController", @{@"title":refundLabel.text});
        }
    }];
    [rightView addGestureRecognizer:tapRight];

    [self observeSetup];
}

- (void)observeSetup
{
    @weakify(self);
    //登录监听
    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGIN_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        [self reloadData];
    }];

    //登出监听
    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGOUT_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        [self reloadData];
    }];

    [self reloadData];

}

- (void)reloadData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        OMAccount *account = KTSystemStorageSingleton.account;
        dispatch_async(dispatch_get_main_queue(), ^{

            if (account) {
                [self loadUIWithAccount:account];

            }else {
                [self loadUIWithoutAccount];
            }
        });
    });
}

- (void)loadUIWithAccount:(OMAccount *)account
{
    [self.avatarImageView kt_imageWithURLStr:account.avatar
                                 placeHolder:[UIImage st_imageNamed:@"ic_personal_avatar_placehold.jpg" type:@""]
                                    withSize:CGSizeMake(avatarWidth*3, avatarWidth*3)
                                    progress:nil
                                onCompletion:^(UIImage * _Nullable image, BOOL isSuccess) {

                                }];
    _toLoginButton.hidden = true;
    _nicknameLb.hidden = NO;
    _nicknameLb.text = account.nickname;
}

- (void)loadUIWithoutAccount
{
    _toLoginButton.hidden = false;
    _nicknameLb.text = @"未登录";
    _nicknameLb.hidden = true;
    _avatarImageView.image = [UIImage st_imageNamed:@"ic_personal_avatar_placehold.jpg" type:@""];
}



@end
